
package com.csc440.team2.smil;

import java.io.IOException;
import java.io.Serializable;
import java.io.StringWriter;
import java.util.ArrayList;


import org.xmlpull.v1.XmlSerializer;


import android.util.Log;
import android.util.Xml;


import com.csc440.team2.SmilConstants;


public class SequenceSeries extends SMILSeries implements Serializable
{
    private static final long serialVersionUID = 1L;


    public static SequenceSeries create()
    {
        SequenceSeries series = new SequenceSeries(SmilConstants.SERIES_TYPE_SEQUENCE);
        series.setComponentList(new ArrayList<SMILComponent>());
        return series;
    }


    private SequenceSeries(int type)
    {
        setType(type);
    }


    public String getXml()
    {
        XmlSerializer serializer = Xml.newSerializer();
        try
        {
            StringWriter writer = new StringWriter();

            serializer.setOutput(writer);
            // set indentation option
            serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);

            serializer.startTag(null, "seq");
            serializer.attribute(null, "dur", "" + getDuration());

            for (SMILComponent comp : getComponentList())
            {
                switch (comp.getType())
                {
                    case SmilConstants.COMPONENT_TYPE_TEXT:
                    {
                        SMILTextComponent textComponent = (SMILTextComponent) comp;
                        serializer.startTag(null, "text");
                        serializer.attribute(null, "dur", "" + textComponent.getDuration());
                        serializer.text(textComponent.getText());
                        serializer.endTag(null, "text");
                        break;
                    }
                    case SmilConstants.COMPONENT_TYPE_IMAGE:
                    {
                        SMILImageComponent imageComponent = (SMILImageComponent) comp;
                        serializer.startTag(null, "img");
                        serializer.attribute(null, "dur", "" + imageComponent.getDuration());
                        serializer.text(imageComponent.getText());
                        serializer.endTag(null, "img");
                        break;
                    }
                    case SmilConstants.COMPONENT_TYPE_AUDIO:
                    {
                        SMILAudioComponent audioComponent = (SMILAudioComponent) comp;
                        serializer.startTag(null, "audio");
                        serializer.attribute(null, "dur", "" + audioComponent.getDuration());
                        serializer.text(audioComponent.getText());
                        serializer.endTag(null, "audio");
                        break;
                    }
                    case SmilConstants.COMPONENT_TYPE_VIDEO:
                    {
                        SMILVideoComponent videoComponent = (SMILVideoComponent) comp;
                        serializer.startTag(null, "video");
                        serializer.attribute(null, "dur", "" + videoComponent.getDuration());
                        serializer.text(videoComponent.getText());
                        serializer.endTag(null, "video");
                        break;
                    }
                }
            }

            serializer.endTag(null, "seq");

            serializer.endDocument();
            serializer.flush();

            return writer.toString();
        }
        catch (IOException ioe)
        {
            Log.e("IOException", "Error serializing XML", ioe);
            return "";
        }
    }
}
